{% extends 'myadmin/index.html'%}

{% block title %}
<title>后台添加会员</title>
{% endblock %}

{% block con %}

<div class="row">
        <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
            <div class="widget am-cf">
                <div class="widget-head am-cf">
                    <div class="widget-title am-fl">
                        添加用户
                    </div>
                    <div class="widget-function am-fr">
                        <a href="javascript:;" class="am-icon-cog">
                        </a>
                    </div>
                </div>
                <div class="widget-body am-fr">

                    <form action="{% url 'user_insert' %}" method="POST" enctype="multipart/form-data" class="am-form tpl-form-line-form">
                        {% csrf_token%}
                        <div class="am-form-group">
                            <label for="user-name" class="am-u-sm-3 am-form-label">昵称</label>
                            <div class="am-u-sm-9">
                                <input type="text" name="nikename" class="tplf-form-input" placeholder="请输入昵称">
                            </div>
                        </div>
                        <div class="am-form-group">
                            <label for="user-name" class="am-u-sm-3 am-form-label">密码</label>
                            <div class="am-u-sm-9">
                                <input type="password" name="password" class="tplf-form-input" placeholder="请输入密码">
                            </div>
                        </div>
                        <div class="am-form-group">
                            <label for="user-name" class="am-u-sm-3 am-form-label">手机</label>
                            <div class="am-u-sm-9">
                                <input type="number" name="phone" class="tplf-form-input" placeholder="请输入手机号">
                            </div>
                        </div>
                        <div class="am-form-group">
                            <label for="user-name" class="am-u-sm-3 am-form-label">邮箱</label>
                            <div class="am-u-sm-9">
                                <input type="email" name="email" class="tplf-form-input" placeholder="请输入邮箱">
                            </div>
                        </div>
                        <div class="am-form-group">
                            <label for="user-name" class="am-u-sm-3 am-form-label">年龄</label>
                            <div class="am-u-sm-9">
                                <input type="number" name="age" class="tplf-form-input" placeholder="请输入年龄">
                            </div>
                        </div>
                        <div class="am-form-group">
                            <label for="user-name" class="am-u-sm-3 am-form-label">性别</label>
                            <div class="am-u-sm-9">
                                <label class="am-radio-inline"><input type="radio" value="0" name="sex" >女</label>
                                <label class="am-radio-inline"><input type="radio" value="1" name="sex" checked>男</label>
                            </div>
                        </div>
                        <div class="am-form-group">
                            <label for="user-weibo" class="am-u-sm-3 am-form-label">
                                上传头像
                                <span class="tpl-form-line-small-title">
                                    Images
                                </span>
                            </label>
                            <div class="am-u-sm-9">
                                <div class="am-form-group am-form-file">
                                    <div class="tpl-form-file-img">
                                        <!--<img src="/static/myadmin/assets/img/a5.png" alt="">-->
                                         <img src="" alt="" id="showImg" width="100">
                                    </div>
                                    <button type="button" class="am-btn am-btn-danger am-btn-sm">
                                        <i class="am-icon-cloud-upload">
                                        </i>
                                        添加头像
                                    </button>
                                    <!--<input id="doc-form-file" name="pic" type="file" multiple="">-->
                                    <input type="hidden" id="img" />
		                            <input type="file" id="uploadImg" onchange="selectImg(this);" name="pic" multiple=""/>
                                </div>
                            </div>
                        </div>
                        <div class="am-form-group">
                            <div class="am-u-sm-9 am-u-sm-push-3">
                                <button class="am-btn am-btn-primary tpl-btn-bg-color-success ">
                                    提交
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
<script>
 $(document).ready(function(){
   $('#huiyuanguanli').click()
})
var image = '';
	var canvas;
	var base64; //将canvas压缩为base64格式
	function selectImg(file) {
		if (!file.files || !file.files[0]) {
			return;
		}
		var reader = new FileReader(); //读取文件
		reader.onload = function(event) { //文件读取完成的回调函数
			image = document.getElementById('showImg');
			image.src = event.target.result; //读入文件的base64数据(可直接作为src属性来显示图片)
			//alert(event.target.result);
			//图片读取完成的回调函数（必须加上否则数据读入不完整导致出错！）
			image.onload = function() {
				canvas = convertImageToCanvas(image); //图片转canvas
				base64 = canvas.toDataURL('image/jpeg'); //将图片数据转为base64.
				//alert(base64);
				$.post(
					"/server_interface_url/", //服务器接口(返回图片路径)
					{
						data: base64
					},
					function(data) {
						alert(data.target);
						//alert(eval(data));
						//修改上传文件的路径名字(图片完整路径)
						$('#img').val('http://path/' + data.target);
					}, "json");
			}
		}
		//将文件已Data URL的形式读入页面
		reader.readAsDataURL(file.files[0]);
	}
	// 把image 转换为 canvas对象
	function convertImageToCanvas(image) {
		// 创建canvas DOM元素，并设置其宽高和图片一样
		var canvas = document.createElement("canvas");
		canvas.width = image.width;
		canvas.height = image.height;
		// 坐标(0,0) 表示从此处开始绘制，相当于偏移。
		canvas.getContext("2d").drawImage(image, 0, 0);

		return canvas;
	}
</script>
{% endblock %}

